/**
 * 详情对话框
 */
var FlowOrderInfoDlg = {
    data: {
        orderId: "",
        orderCode: "",
        payCode: "",
        orderType: "",
        name: "",
        phone: "",
        serviceCity: "",
        storageTime: "",
        begAddress: "",
        endAddress: "",
        orderDate: "",
        moneys: "",
        payMoneys: "",
        bdMemberId: "",
        zfbMemberId: "",
        settlementMoneys: "",
        payStatus: "",
        begLocation: "",
        endLocation: "",
        kilometer: "",
        isRefund: "",
        refundCode: "",
        orderStatus: "",
        allPayMoneys: "",
        addMoneys:"",
        notes: "",
        endNotes: "",
        orderFrom: "",
        useDate: "",
        kilometerNote:"",
        storeId:""
    }
};

var PFun = {};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;



















































































    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/flowOrder/detail?orderId=" + Feng.getUrlParam("orderId"));
    var result = ajax.start();
    form.val('flowOrderForm', result.data);
    //填满表单
    $("#orderCode").html(result.data.orderCode);
    $("#begAddress").html(result.data.begAddress);
    $("#endAddress").html(result.data.endAddress);
    $("#serviceCity").html(result.data.serviceCity);
    $("#serviceName").html(result.data.orderTypeName + " " +result.data.serviceName);
    $("#name").html(result.data.name);
    $("#phone").html(result.data.phone);

    if(result.data.flowOrderDis){
        $("#couponId").val(result.data.flowOrderDis.couponId);
        $("#couponMoneys").val(result.data.flowOrderDis.disMoneys);
        $("#couponName").val(result.data.flowOrderDis.note);
    }
    console.log("sssss1",result.data.depositPayCode)

    if(result.data.depositPayCode){
        console.log("sssss",result.data.depositPayCode)
        $("#deposit").val(result.data.deposit);
    }else {
        $("#deposit").val(0);
    }

    if(result.data.flowOrderGoods && result.data.flowOrderGoods.length > 0){
        for (var i = 0;i<result.data.flowOrderGoods.length;i++){
            $("#btnAdd").parent().before("<div class=\"param-line layui-col-md12\">\n" +
                "                                <div class=\"layui-inline\">\n" +
                "                                    <input type=\"text\" class=\"layui-input param-name\" placeholder=\"填写或选择\" value=\""+result.data.flowOrderGoods[i].goodsName+"\">\n" +
                "<input type=\"hidden\" class=\"param-id\" value='"+result.data.flowOrderGoods[i].goodsId+"' />" +
                "                                </div>\n" +
                "                                <div class=\"layui-inline\">\n" +
                "                                    <input type=\"text\" class=\"layui-input param-type\" value='"+result.data.flowOrderGoods[i].typeName+"' readonly='readonly' placeholder=\"物品分类\">\n" +
                "                                </div>\n" +
                "                                <div class=\"layui-inline\">\n" +
                "                                    <button class=\"layui-btn layui-btn-primary param-btn\">从系统选择</button>\n" +
                "                                </div>\n" +
                "                                <div class=\"layui-inline\">\n" +
                "                                    <input type=\"text\" style='width: 100px;' class=\"layui-input param-price\" placeholder=\"物品单价\" value=\""+result.data.flowOrderGoods[i].goodsPrice+"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"layui-inline\">\n" +
                "                                    <input type=\"text\" style='width: 100px;' class=\"layui-input param-count\" placeholder=\"物品数量\" value=\""+result.data.flowOrderGoods[i].goodsCount+"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"layui-inline\">\n" +
                "                                    <i class=\"layui-icon layui-icon-close\" style=\"font-size: 30px; color: #cc0000;\"></i>\n" +
                "                                </div>\n" +
                "                            </div>");
        }

        form.render();
    }

    //结束
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        //优惠卷
        data.field.couponId = $("#couponId").val();
        data.field.kilometerNote = escape($("#kilometerNote").val());
        var goods = "";
        $("#formList").find('.param-line').each(function () {
            var node = $(this);
            var name = node.find('input.param-name').val();
            var price = node.find('input.param-price').val();
            var count = node.find('input.param-count').val();
            var pid = node.find('input.param-id').val();
            //商品名称#商品id#商品数量#商品价格,
            if(goods){
                goods += ","+name+"#"+pid+"#"+count+"#"+price;
            }else {
                goods += ","+name+"#"+pid+"#"+count+"#"+price;
            }
        });
        data.field.cGoods = escape(goods);
        //物品价格

        var ajax = new $ax(Feng.ctxPath + "/flowOrder/editItem", function (data) {
            if(data.code == 200){
                Feng.success("更新成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }else{
                Feng.error("更新失败！" + data.message)
            }

        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $("#btnAdd").click(function () {
        $(this).parent().before("<div class=\"param-line layui-col-md12\">\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" class=\"layui-input param-name\" placeholder=\"填写或选择\">\n" +
            "<input type=\"hidden\" class=\"param-id\" value='-1' />" +
            "                                </div>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" class=\"layui-input param-type\" value='' readonly='readonly' placeholder=\"物品分类\">\n" +
            "                                </div>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <button class=\"layui-btn layui-btn-primary param-btn\">从系统选择</button>\n" +
            "                                </div>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" style='width: 100px;' class=\"layui-input param-price\" value='0' placeholder=\"物品单价\">\n" +
            "                                </div>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" style='width: 100px;' class=\"layui-input param-count\" value='1' placeholder=\"物品数量\">\n" +
            "                                </div>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <i class=\"layui-icon layui-icon-close\" style=\"font-size: 30px; color: #cc0000;\"></i>\n" +
            "                                </div>\n" +
            "                            </div>");
        form.render();
        return false;
    });

    $("#formList").on('click','.layui-icon-close',function () {
        $(this).parents('.param-line').remove();
    });

    $("#formList").on('click','.param-btn',function () {
        var storeId = $("#storeId").val() || -1;
        layui.layer.open({
            skin : 'layui-layer-admin',
            title: '选择商品',
            content: Feng.ctxPath + '/flowOrder/flowGoods?storeId=' + storeId,
            type:2,
            area:['90%','90%']
        });
        var self = this;
        PFun.chooseShop = function (goodsId,goodsName,goodsPrice,typeName) {
            var node = $(self).parents(".param-line");
            node.find(".param-name").val(goodsName);
            node.find(".param-id").val(goodsId);
            node.find('.param-price').val(goodsPrice);
            node.find(".param-type").val(typeName);
        };
        return false;
    });

    $("#couponName").click(function () {
        var couponId = $("#couponId").val();
        if(couponId == -1 || couponId == ""){
            var zfbMemberId = $("#zfbMemberId").val() || -1;
            var bdMemberId = $("#bdMemberId").val() || -1;
            var orderType = $("#orderType").val() || -1;
            //点击选择
            layui.layer.open({
                skin : 'layui-layer-admin',
                title: '选择优惠卷',
                content: Feng.ctxPath + '/flowOrder/flowCoupon?zfbMemberId='+zfbMemberId+ "&bdMemberId="+bdMemberId+"&serviceType="+orderType,
                type:2,
                area:['90%','90%']
            });
            PFun.chooseCoupon = function (couponId,couponTitle,couponMoneys) {
               $("#couponId").val(couponId);
                $("#couponName").val(couponTitle);
                $("#couponMoneys").val(couponMoneys);
                $("#btnCanselCoupon").show();
            };
        }else {
            Feng.error("已有使用优惠卷，不能选择");
        }
    });

    $("#btnCanselCoupon").click(function () {
        $("#couponId").val('');
        $("#couponName").val('');
        $("#couponMoneys").val('');
        $("#btnCanselCoupon").hide();
        return false;
    });
});